package ro.prjmanager.web.actions.gen;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import ro.prjmanager.model.Groups;
import ro.prjmanager.service.CompGeneratorServiceInterface;
import ro.prjmanager.web.actions.GenericAction;
import ro.prjmanager.web.annotation.DefaultTargetMethod;
import ro.prjmanager.web.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service(target = "genService")
@DefaultTargetMethod(target ="generate",resource = "/index.jsp")

public class GenerateAction extends GenericAction {
    private String comments;
    private List<Integer> groupId;
    private List<Groups> groupsList;

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public List<Groups> getGroupsList() {
        return groupsList;
    }

    public void setGroupsList(List<Groups> groupsList) {
        this.groupsList = groupsList;
    }

    public List<Integer> getGroupId() {
        return groupId;
    }

    public void setGroupId(List<Integer> groupId) {
        this.groupId = groupId;
    }

    @HandlesEvent("showAdd")
    public Resolution load(){
        setGroupsList(getService("groupsService").list());
        ArrayList<Integer> ids=new ArrayList<Integer>();
        setGroupId(ids);
        return  new ForwardResolution("/pages/nomenclators/generate/generateJsp.jsp");

    }
    


    public void generate(){
        CompGeneratorServiceInterface service= (CompGeneratorServiceInterface) getService();

        if(groupId!=null){
            for(Integer idGrp:groupId){
                Groups groups= (Groups) getService("groupsService").load(idGrp);
                service.generate(groups, getContext().getUserSession(),comments);
            }
        }


    }

}
